package org.xbzheng.restful_study.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.xbzheng.restful_study.model.Room;
import org.xbzheng.restful_study.model.RoomColumn;
import org.xbzheng.restful_study.service.RoomService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Created by szheng on 2016/11/1.
 */
@Controller
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @RequestMapping(value = "/{roomId}", method = RequestMethod.GET)
    public String room(@PathVariable final int roomId, HttpServletRequest request){
        final Room room = roomService.getRoom(roomId);

        request.setAttribute("room", room);
        return "room";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public String create(@RequestBody final List<RoomColumn> roomColumns, HttpServletRequest request){
        List<RoomColumn> results = null;
        try{
            results = roomService.createRoom(roomColumns);
            request.getSession(false).setAttribute("roomColums", results);
        }catch (Exception e){
            e.printStackTrace();
        }

        if(results != null){
            return "success";
        }
        return "failed";
    }

}
